/**
 * Assignment 2 - Pub crawl
 * Authors: Huy Nguyen (3430069)    James Gregory (3288801)
 */
#include <stdio.h>
#include <stdlib.h>
#include "pub.h"

extern int *bodyguard_secret_list_of_drunk_delegates;
extern int *sent_backs;
extern int my_id;
extern int delegate_sober, bodyguard_sober, num_delegates;

static int last_bodyguard_pub_vote_to_sober_delegate = 0;

int delegate_pub_vote(void) {
    return (rand() % 2);
}

int bodyguard_pub_vote(int delegate_group_id) {
    // delegate is drunk, don't care.
    if(bodyguard_secret_list_of_drunk_delegates[delegate_group_id]) {
        return 0;
    }
    // delegate is not drunk, send a different vote to the one that also not drunk
    last_bodyguard_pub_vote_to_sober_delegate = !last_bodyguard_pub_vote_to_sober_delegate;
    return last_bodyguard_pub_vote_to_sober_delegate;
}

int delegate_wench_vote(int *wench_nominations) {
    // both nominations are bodyguard, don't care
    if (wench_nominations[0] > BODYGUARD_ID_OFFSET && wench_nominations[1] > BODYGUARD_ID_OFFSET) {
        return (rand() % 2);
    }
    // both nominations are delegates, don't care
    if (wench_nominations[0] < BODYGUARD_ID_OFFSET && wench_nominations[1] < BODYGUARD_ID_OFFSET) {
        return (rand() % 2);
    }
    // always vote for bodyguard to be wench
    return wench_nominations[1] > wench_nominations[0];
}

int bodyguard_wench_vote(int delegate_vote, int *wench_nominations, int delegate_group_id) {
    int vote;
    // delegate is drunk, don't care.
    if(bodyguard_secret_list_of_drunk_delegates[delegate_group_id]) {
        vote = delegate_vote;
    } else if(wench_nominations[0] > BODYGUARD_ID_OFFSET && wench_nominations[1] > BODYGUARD_ID_OFFSET) {    // both nominations are bodyguards, don't care
        vote = delegate_vote;
    } else {      // delegate is not drunk, send a different vote to the one that also not drunk
        if (wench_nominations[0] == delegate_group_id + 1) {
            vote = 0;
        } else if (wench_nominations[1] == delegate_group_id + 1) {
            vote = 1;
        } else {
            vote = 0;
        }
    }
    return vote;
}

void bodyguard_process_vote(int *vote, int sender) {
    bodyguard_secret_list_of_drunk_delegates[sender] = vote[1];
}

int bodyguard_vote_secret(void) {
    return delegate_sober < 0;
}

void delegate_nominate(int *wench_nominations) {
    wench_nominations[0] = my_id + BODYGUARD_ID_OFFSET;
    wench_nominations[1] = ((my_id % num_delegates) + 1) + BODYGUARD_ID_OFFSET;
    printf("delegate %d nominates %d and %d\n", my_id, wench_nominations[0], wench_nominations[1]); 
}

/* Nominate two sober delegate */
void bodyguard_nominate(int *wench_nominations) {
    for(int i = 0, j = 0; i < num_delegates && j < 2; i++) {
        if(!bodyguard_secret_list_of_drunk_delegates[i]) {
            wench_nominations[j++] = i+1; 
        }
    }
    printf("bodyguard %d nominates %d and %d\n", my_id + BODYGUARD_ID_OFFSET, wench_nominations[0], wench_nominations[1]); 
}

int tally_votes(int *votes) {
    int count[2] = {0};
    for(int i = 0; i < num_delegates; i++) {
        if (!sent_backs[i]) {
            count[votes[i]]++;
        }
    }
    return (count[1] >= count[0]);
}
